<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">app</div>

  <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js" crossorigin></script>

  <script type="text/babel">
    
    class App extends React.Component {
      constructor() {
        super()

        this.state = {
          message: 'message',
          counter: 0
        }

        // 1.2.在构造器中给方法绑定this
        this.btnClick = this.btnClick.bind(this)
      }

      render() {
        return (
          <div>
            { /*方案一：bind绑定this(显示绑定) this.btnClick.bind(this) */ }
            <button onClick={this.btnClick}>按钮</button>
            { /*方案二：箭头函数 */ }
            <button onClick={this.increment}>+1</button>
            { /*方案三(推荐)：直接传入箭头函数，在箭头函数中调用需要执行的方法 */ }
            <button onClick={() => { this.decrement() }}>-1</button>
          </div>
        )
      }

      btnClick() {
        console.log(this.state.message)
      }
      
      // increment() {
      //   console.log(this.state.counter)
      // }

      // 2.使用箭头函数 永远不绑定this
      increment = () => {
        this.setState({
          counter: this.state.counter + 1
        })
        console.log(this.state.counter)
      }

      decrement() {
        this.setState({
          counter: this.state.counter - 1
        })
        console.log(this.state.counter)
      }
    }

    ReactDOM.render(<App />, document.getElementById('app'))
  </script>
</body>
</html>